package flib;

import java.awt.geom.*;
import java.io.File;
import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PImage;
import simpleGeom.Rect2D;

public class PNGArranger extends PApplet {
	Rect2D bounds;
	Rect2D[] rects;
	PImage[] imgs;
	PImage canvas;
	float[] transX;
	float[] transY;
	float[] rots;
	boolean[][] grid;
	float fitness;
	int nX = 80;
	int nY = 300;

	public void setup() {
		size(320, 800);
		bounds = new Rect2D(0, 0, 10000, 10000);
		canvas = this.createImage(10000, 10000, RGB);
		for (int x = 0; x < canvas.width; x++) {
			for (int y = 0; y < canvas.height; y++) {
				canvas.set(x, y, color(255, 0, 0));
			}
		}
		loadImages();
	}

	public void loadImages() {
		File folder = new File("/Users/administrator/Desktop/plansSmall/");
		ArrayList<File> files = listFilesFromFolder(folder, ".png");
		imgs = new PImage[files.size()];
		rects = new Rect2D[files.size()];
		for (int i = 0; i < files.size(); i++) {
			println("i:" + i);
			File file = files.get(i);
			imgs[i] = loadImage(file.getAbsolutePath());
			// rects[i]=new Rect2D(0,0,imgs[i].width,imgs[i].height);
			place(i);
			canvas.copy(imgs[i], 0, 0, imgs[i].width, imgs[i].height,
					(int) rects[i].x1, (int) rects[i].y1, imgs[i].width,
					imgs[i].height);
		}
		canvas.save("/Users/administrator/Desktop/canvas.png");
	}

	public void place(int index) {
		PImage img = imgs[index];
		for (int y = 0; y < bounds.getDimY() - img.height; y += 8) {
			for (int x = 0; x < bounds.getDimX() - img.width; x += 8) {
				Rect2D iR = new Rect2D(x, y, x + img.width, y + img.height);

				if (isFit(iR, index)) {
					rects[index] = iR;
					println("fits");
					return;
				}
			}
		}
		println("not fitted");
	}

	public boolean isFit(Rect2D iR, int index) {
		for (int i = 0; i < index; i++) {
			Rect2D r = rects[i];
			if (r.intersects(iR)) {
				/*
				 * println("r1"+r); println("r"+iR);
				 */
				return false;
			}
		}
		return true;
	}

	public void draw() {

	}

	/**
	 * check if there is any overlap with another piece
	 */
	public boolean fit(boolean[][] shape, int x1, int y1) {
		if (x1 < 0 || y1 < 0 || shape.length + x1 > grid.length
				|| shape[0].length + y1 > grid[0].length)
			return false;
		for (int x = 0; x < shape.length; x++) {
			for (int y = 0; y < shape[0].length; y++) {
				if (shape[x][y] && grid[x + x1][y + y1]) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * occupy the covered pixels
	 */
	public void place(boolean[][] shape, int x1, int y1) {
		for (int x = 0; x < shape.length; x++) {
			for (int y = 0; y < shape[0].length; y++) {
				if (shape[x][y]) {
					grid[x + x1][y + y1] = true;
				}
			}
		}
	}

	/**
	 * change the sequence of the pieces
	 */
	public void swapPolys(int i1, int i2) {

	}

	/**
	 * change the sequence of the pieces evaluate the new fitness keep if new
	 * version is better
	 */
	public void evolve() {

	}

	/**
	 * fitness=filled height of the pieces
	 */
	public float fitness() {
		for (int y = grid[0].length - 1; y >= 0; y--) {
			for (int x = 0; x < grid.length; x++) {
				if (grid[x][y])
					return y;
			}
		}
		return 0;
	}

	public static ArrayList<File> listFilesFromFolder(final File folder,
			String fileEnd) {
		ArrayList<File> files = new ArrayList<File>();
		for (final File fileEntry : folder.listFiles()) {
			if (fileEntry.isFile()) {
				String temp = fileEntry.getName();
				if (temp.toLowerCase().endsWith(fileEnd)) {
					files.add(fileEntry);
				}
			}
		}
		return files;
	}

}
